<template>
  <div class="app-container">
    <el-tabs v-model="listQuery.status" type="card" style="margin-bottom:10px">
      <el-tab-pane
        v-for="item in jcRegStatusJson"
        :key="item.value"
        :label="item.label"
        :name="item.value+''"
      ></el-tab-pane>
    </el-tabs>
    <div class="filter-container">
      <el-input v-model="listQuery.focusIdentifier" placeholder="集采编号" class="filter-item" clearable />
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >查询</el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      row-key="id"
      style="width: 100%;"
    >
      <el-table-column label="序号" type="index" align="center" width="60px" />
      <el-table-column label="集采编号" prop="focusIdentifier" align="center" />
      <el-table-column label="企业名称" prop="companyName" align="center" />
      <el-table-column label="产品名称" prop="name" align="center" />
      <el-table-column label="单位" prop="unit" align="center" />
      <el-table-column label="采购价格" prop="topPrice" align="center" />
      <el-table-column label="报名时间" prop="createTime" align="center" width="160px" />
      <el-table-column label="报名数量" prop="num" align="center" />
      <el-table-column label="状态" prop="statusText" align="center">
        <template slot-scope="{row}">
          <el-tag :type="row.status | statusFilter">
            {{ row.statusText }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="220px"
      >
        <template slot-scope="{row}">
          <el-button
            type="primary"
            size="mini"
            @click="handleCancel(row)"
            v-if="row.status === 2 "
          >取消报名</el-button>
          <!-- 不要详情 -->
          <!-- <el-button size="mini" @click="handleDetail(row)">详情</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.pageNo"
      :limit.sync="listQuery.pageSize"
      @pagination="getList"
    />

    <el-dialog title="详情" :visible.sync="dialogDetailVisible" width="888px" v-if="detail">
      <el-row :gutter="40">
        <el-col :span="8">
          集采编号：
          <span style="font-size:13px">{{detail.focusIdentifier}}</span>
        </el-col>
        <el-col :span="8">创建时间：{{detail.createTime}}</el-col>
        <el-col :span="8">订单状态：{{detail.statusText}}</el-col>
      </el-row>
      <el-row :gutter="40">
        <el-col :span="8">名称：{{detail.name}}</el-col>
        <el-col :span="8">单位：{{detail.unit}}</el-col>
        <el-col :span="8">采购价格：{{detail.topPrice}}</el-col>
      </el-row>
      <el-row :gutter="40">
        <el-col :span="24">集采时间：{{detail.startDate}}至{{detail.endDate}}</el-col>
      </el-row>
      <el-row :gutter="40">
        <el-col :span="8">成团数量：{{detail.totalNum}}</el-col>
        <el-col :span="8">已成团数量：{{detail.finishNum}}</el-col>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogDetailVisible = false">取消</el-button>
        <el-button type="primary" @click="dialogDetailVisible = false">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  focusDetailPageByQo,
  focusGetById,
  focusDetailCancel
} from "@/api/transaction";
import waves from "@/directive/waves"; // waves directive
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination
import { jcRegStatusJson } from "../constant";
import NumInput from "@/components/NumInput";
import DatePicker from "@/components/DatePicker";

export default {
  name: "TransactionGroupReg",
  directives: { waves },
  components: { Pagination, NumInput, DatePicker },
  filters: {
    statusFilter(status) { //状态
      const statusMap = {
        2: '',
        3: 'success',
        99: 'danger',
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        status: "2",
        pageNo: 1,
        pageSize: 20,
        focusIdentifier: ""
      },
      jcRegStatusJson,
      dialogDetailVisible: false,
      detail: {}
    };
  },
  watch: {
    "listQuery.status": function() {
      this.handleFilter();
    }
  },
  created() {
    if (this.$route.query.status && this.listQuery.status !== this.$route.query.status.toString()) {
      this.listQuery.status = this.$route.query.status.toString()
    }
    if (this.$route.query.identifier) {
      this.listQuery.focusIdentifier = this.$route.query.identifier.toString();
    }

    this.getList();
  },
  methods: {
    getList() {
      this.listLoading = true;
      focusDetailPageByQo(this.listQuery).then(response => {
        this.list = response.list || [];
        this.total = response.totalCount;
        this.listLoading = false;
      });
    },
    handleFilter() {
      this.listQuery.pageNo = 1;
      this.getList();
    },
    handleDetail(row) {
      this.getDetail(row.id).then(res => {
        this.detail = Object.assign({}, res);
        this.dialogDetailVisible = true;
      });
    },
    getDetail(id) {
      return new Promise((resolve, reject) => {
        focusGetById(id)
          .then(res => {
            resolve(res);
          })
          .catch(err => {
            reject(err);
          });
      });
    },
    handleCancel(row) {
      this.$confirm("是否确认取消报名？").then(() => {
        focusDetailCancel({ focusId: row.focusId }).then(() => {
          // this.$notify({
          //   title: "成功",
          //   message: "操作成功！",
          //   type: "success",
          //   duration: 2000
          // });

          this.$message({
            message: '操作成功！',
            type: 'success'
          });
          this.getList();
        });
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.el-row {
  line-height: 2;
  font-size: 15px;
}
</style>